const express = require('express')
const boom = require('boom')

const userRouter = require('./user')
const bookRouter = require('./book')
const {CODE_ERROR} = require('../utils/constant')
const jwtAuth = require('./jwt')
const Result = require('../controllers/Result')

// 注册路由
const router = express.Router()

router.use(jwtAuth)


router.get('/', (req, res) => {
  res.send('get')
})

router.use('/user', userRouter)
router.use('/book', bookRouter)

/**
 * 集中处理404请求的中间件
 * 必须放在正常处理流程之后，否则会拦截正常请求
 */
router.use((req, res, next) => {
  console.log(1111)
  next(boom.notFound('接口不存在'))
})


/**
 * 自定义路由异常处理中间件
 * 1.方法的参数不能少
 * 2. 必须放在路由的最后
 * 
 * 因为中间件是逐一向下执行的
 */
 router.use((err, req, res, next) => {
  console.log(err)
  if (err.name && err.name === 'UnauthorizedError') {
    const { status = 401, message } = err
    new Result(null, 'Token验证失败', {
      error: status,
      errMsg: message
    }).jwtError(res.status(status))
  } else {
    const msg = (err && err.message) || '系统错误'
    const statusCode = (err.output && err.output.statusCode) || 500;
    const errorMsg = (err.output && err.output.payload && err.output.payload.error) || err.message
    new Result(null, msg, {
      error: statusCode,
      errorMsg
    }).fail(res.status(statusCode))
  }
})

module.exports = router